package models;

import com.avaje.ebean.Page;
import models.core.GipxyModel;
import models.production.Task;
import play.data.validation.Constraints;
import play.db.ebean.Model;

import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Huyen
 * Date: 4/21/12
 * Time: 2:27 PM
 * To change this template use File | Settings | File Templates.
 */
@Entity
public class Product extends GipxyModel {
    @Constraints.Required
    public String code;

    @Constraints.Required
    public String name;

    public String customer;

    public String detail;

    //thu tu la quan trong
    public List<Task> tasks;

    public static Finder<Long,Product> find = new Finder<Long,Product>(Long.class, Product.class);

    /**
     * Return a page of computer
     *
     * @param page Page to display
     * @param pageSize Number of product per page
     * @param sortBy product property used for sorting
     * @param order Sort order (either or asc or desc)
     * @param filter Filter applied on the name column
     */
    public static Page<Product> page(int page, int pageSize, String sortBy, String order, String filter) {
        System.out.println("bbbb:"+filter);
        return find.where()
                .ilike("code", "%" + filter + "%")
                .orderBy(sortBy + " " + order)
                .findPagingList(pageSize)
                .getPage(page);
    }

    public static Map<String,String> options() {
        LinkedHashMap<String,String> options = new LinkedHashMap<String,String>();
        for(Product c: Product.find.orderBy("name").findList()) {
            options.put(c.code.toString(), c.name);
        }
        return options;
    }

    public static List<Product> all(){
        return find.all();
    }
}
